package com.zlf.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.zlf.constant.CrowdConstant;
import com.zlf.entity.Admin;
import com.zlf.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ：张利锋
 * @date ：Created in 2020/11/12 21:11
 * @description： TOOD
 * @version: 1.0
 */
@Controller
public class AdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("/admin/do/edit")
    public String doEdit(Admin admin, @RequestParam("pageNum") String pageNum,
                         @RequestParam("keyword") String keyword) {
        adminService.updateAdmin(admin);
        return "redirect:/admin/page?keyword=" + keyword + "&pageNum=" + pageNum;
    }

    //此处不能用restful，否则无法传递keyword等参数
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {
        Admin admin = adminService.getAdminById(adminId);
        model.addAttribute(CrowdConstant.ATTR_NAME_ADMIN, admin);
        return "admin-edit";
    }

    @RequestMapping("/admin/do/add")
    public String addAdmin(Admin admin) {
        adminService.saveAdmin(admin);
        return "redirect:/admin/page?pageNum=" + Integer.MAX_VALUE;
    }

    @RequestMapping(value = {"/admin/remove/{adminId}/{pageNum}/{keyword}", "/admin/remove/{adminId}/{pageNum}"})
    public String remove(@PathVariable("adminId") Integer adminId,
                         @PathVariable("pageNum") Integer pageNum,
                         @PathVariable(value = "keyword", required = false) String keyword) {
        adminService.deleteByPrimaryKey(adminId);
        //keyword为null时不会默认赋值为”“
        if (keyword == null) {
            keyword = "";
        }
        return "redirect:/admin/page?keyword=" + keyword + "&pageNum=" + pageNum;
    }

    @RequestMapping("/admin/remove/all")
    public String removeAll(@RequestParam("adminList")List<Integer> adminList,
                            @RequestParam(value = "keyword", defaultValue = "") String keyword,
                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        for (Integer adminId:adminList){
            adminService.deleteByPrimaryKey(adminId);
            }
        return "redirect:/admin/page?keyword=" + keyword + "&pageNum=" + pageNum;
    }

    @RequestMapping("/admin/page")
    @PreAuthorize("hasRole('部长') or hasRole('经理')")
    public String geyPageInfo(Model model, @RequestParam(value = "keyword", defaultValue = "") String keyword,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<Admin> pageInfo = adminService.selectAdminListByKeyword(keyword, pageNum, pageSize);
        model.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }
    //登出已托管给spring——security
    @RequestMapping("/admin/do/logout")
    public String doLogout(HttpSession session) {
        session.invalidate();
        return "redirect:/admin/to/login/page";
    }
    //登录已托管给spring——security
    @RequestMapping("/admin/do/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession session) {
        Admin admin = adminService.getAdminByLoginAcct(loginAcct, userPswd);
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page";
    }

}
